package com.angel.core.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.health.SMS;

/**
 * @author cheng sir 2016年9月8日下午3:40:56
 */
public class SMSUtils {

	public static JsonEntity send(SMS sms,String method) {
		try {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("uid", "1"));
			nvps.add(new BasicNameValuePair("username", PropertyConfUtil.getContextProperty(Constants.SEND_SMS_USERNAME)));
			nvps.add(new BasicNameValuePair("password", MD5Util.getMD5(PropertyConfUtil.getContextProperty(Constants.SEND_SMS_PASSWORD))));
			nvps.add(new BasicNameValuePair("mobiles", sms.getMobiles()));
			nvps.add(new BasicNameValuePair("content", sms.getContent()));

			String url = PropertyConfUtil.getContextProperty(Constants.SEND_SMS_URL);
			String post = HttpRequest.toPost(url+method, null, nvps);
			System.out.println(post);
			String[] _post = post.split(":");
			if(_post.length >1){
				return new JsonEntity(_post[0], _post[1].split(",")[0]);
			}else{
				return new JsonEntity("-1", "发送失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return new JsonEntity("-1", "发送失败！");
		}
	}
	
	public static void main(String[] args) throws Exception {
		String single = Constants.SEND_SMS_SINGLE;
//		send(new SMS("1","18510971687","1234为您在天使健康的注册验证码【天使健康】","1",""),single);
		
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("uid", "1"));
		nvps.add(new BasicNameValuePair("username", "tsjk"));
		nvps.add(new BasicNameValuePair("password", MD5Util.getMD5("123465")));
		nvps.add(new BasicNameValuePair("mobiles", "18510971687"));
		nvps.add(new BasicNameValuePair("content", "1234为您在天使健康的注册验证码【天使健康】"));

		String post = HttpRequest.toPost("http://v.369sms.com/SMS/"+single, null, nvps);
		System.err.println(post);
	}
}
